#ifndef FEEDBACKFORM_H
#define	FEEDBACKFORM_H

using namespace std;

class FeedBackForm{
private:
    vector<int> ansVec;
public:
    FeedBackForm();
    void doFeedBackForm(string, int);
    void viewTopicSummary(string);
};

FeedBackForm::FeedBackForm(){
    ansVec.clear();
}

void FeedBackForm::doFeedBackForm(string topicCode, int userid){
    string qn, ansStr, zSQLstr, zSQLstr2, existQn;
    stringstream ss, ss2;
    int ans;
    Question qnObj;
    
    Database *db;
    db = new Database("test.db");
    
    ss << "select * from " << topicCode << ";";
       
    zSQLstr = ss.str();
    
    char* zSQL = (char*)zSQLstr.c_str();
    
    vector<vector<string> > result = db->query(zSQL);

    for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it){
        vector<string> row = *it;
        existQn = row.at(0);
    }
    
    if(existQn == ""){
        if(db->getFail() == 1){
            cout << "No such topic in the system" << endl;
        }else{
            cout << "No questions in the topic" << endl;
        }
    }else{
        for(int i = 0; i < 20; i++){
        qn = qnObj.retrieveQuestion(topicCode, i+1);
        cout << "Question " << i+1 << " : " << qn << endl;
        cout << "1. Very Good\t2. Good\t3. Neutral\t4. Bad\t5. Very Bad" << endl;
        
        while (1)
        {
        cout << "Ans : ";
        getline(cin, ansStr);
        cout << endl;
        
        if (ansStr == "1" || ansStr == "2" || ansStr == "3" || ansStr == "4" || ansStr == "5")
            break;
        }
        ans = atoi(ansStr.c_str());
        ansVec.push_back(ans);
        }

        ss2 << "insert into ans_" << topicCode << " values ("
        << userid << "," 
        << ansVec[0] << "," << ansVec[1] << ","
        << ansVec[2] << "," << ansVec[3] << ","
        << ansVec[4] << "," << ansVec[5] << ","
        << ansVec[6] << "," << ansVec[7] << ","
        << ansVec[8] << "," << ansVec[9] << ","
        << ansVec[10] << "," << ansVec[11] << ","
        << ansVec[12] << "," << ansVec[13] << ","
        << ansVec[14] << "," << ansVec[15] << ","
        << ansVec[16] << "," << ansVec[17] << ","
        << ansVec[18] << "," << ansVec[19] << ");";

        zSQLstr2 = ss2.str();

        char* zSQL = (char*)zSQLstr2.c_str();

        db->query(zSQL);
        
        cout << "Thanks for completing the feedback form" << endl;
    }
    db->close();
}

void FeedBackForm::viewTopicSummary(string topicCode){
    string zSQLstr, value;
    stringstream ss;
    float count1, count2, count3, count4, count5;
    vector<string> test;
    
    count1 = 0;
    count2 = 0;
    count3 = 0;
    count4 = 0;
    count5 = 0;
    
    Database *db;
    db = new Database("test.db");
    
    ss << "select * from ans_" << topicCode << ";"; 
       
    zSQLstr = ss.str();

    char* zSQL = (char*)zSQLstr.c_str();
    
    vector<vector<string> > result = db->query(zSQL);

    for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it){
        vector<string> row = *it;
        for(int i = 0; i < 20; i++){
        test.push_back(row.at(i + 1));
        }
    }
    
    if(result.empty()){
        if(db->getFail() == 1){
            cout << "No such topic in the system" << endl;
        }else{
            cout << "No records of attempts for the topic" << endl;
        }
    }else{
        for (int i = 0; i< test.size(); i++)
        {        

            if (test[i] == "5")
            {       
                count5++;
            }  
            else if (test[i] == "4")
            {
                count4++;
            } 
            else if (test[i] == "3")
            {
                count3++;
            } 
            else if (test[i] == "2")
            {
                count2++;
            }   
            else if (test[i] == "1")
            {
                count1++;
            }
        }
        
        cout << "Topic code : " << topicCode << endl << endl;
        cout << "Total amount of attempts = " << (test.size()/ 20) << endl;
        cout << "The % of choice 1 = " << (count1 / test.size()) * 100 << endl; 
        cout << "The % of choice 2 = " << (count2 / test.size()) * 100 << endl;
        cout << "The % of choice 3 = " << (count3 / test.size()) * 100 << endl;
        cout << "The % of choice 4 = " << (count4 / test.size()) * 100 << endl;
        cout << "The % of choice 5 = " << (count5 / test.size()) * 100 << endl;
    }
    
    db->close();
}


#endif	/* FEEDBACKFORM_H */

